home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / tftp / mainserv.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  84 lines

  1. /*
  2.  * tftp - Trivial File Transfer Program.  Server side.
  3.  *
  4.  *    -i        says we were *not* started by inted.
  5.  *    -p port#    specifies a different port# to listen on.
  6.  *    -t        turns on the traceflag - writes to a file.
  7.  */
  8.  
  9. #include    "defs.h"
  10.  
  11. main(argc, argv)
  12. int   argc;
  13. char  **argv;
  14. {
  15.     int        childpid;
  16.     register char    *s;
  17.  
  18.     err_init("rich's tftpd");
  19.  
  20.     while (--argc > 0 && (*++argv)[0] == '-')
  21.         for (s = argv[0]+1; *s != '\0'; s++)
  22.             switch (*s) {
  23.             case 'i':
  24.                 inetdflag = 0;    /* turns OFF the flag */
  25.                         /* (it defaults to 1) */
  26.                 break;
  27.  
  28.             case 'p':        /* specify server's port# */
  29.                 if (--argc <= 0)
  30.                    err_quit("-p requires another argument");
  31.                 port = atoi(*++argv);
  32.                 break;
  33.  
  34.             case 't':
  35.                 traceflag = 1;
  36.                 break;
  37.  
  38.             default:
  39.                 err_quit("unknown command line option: %c", *s);
  40.             }
  41.  
  42.     if (inetdflag == 0) {
  43.         /*
  44.          * Start us up as a daemon process (in the background).
  45.          * Also initialize the network connection - create the socket
  46.          * and bind our well-known address to it.
  47.          */
  48.  
  49.         daemon_start(1);
  50.  
  51.         net_init(TFTP_SERVICE, port);
  52.     }
  53.  
  54.     /*
  55.      * If the traceflag is set, open a log file to write to.
  56.      * This is used by the DEBUG macros.  Note that all the
  57.      * err_XXX() functions still get handled by syslog(3).
  58.      */
  59.  
  60.     if (traceflag) {
  61.         if (freopen(DAEMONLOG, "a", stderr) == NULL)
  62.             err_sys("can't open %s for writing", DAEMONLOG);
  63.         DEBUG2("pid = %d, inetdflag = %d", getpid(), inetdflag);
  64.     }
  65.  
  66.     /*
  67.      * Concurrent server loop.
  68.      * The child created by net_open() handles the client's request.
  69.      * The parent waits for another request.  In the inetd case,
  70.      * the parent from net_open() never returns.
  71.      */
  72.  
  73.     for ( ; ; ) {
  74.         if ( (childpid = net_open(inetdflag)) == 0) {
  75.             fsm_loop(0);    /* child processes client's request */
  76.             net_close();    /* then we're done */
  77.             exit(0);
  78.         }
  79.  
  80.         /* parent waits for another client's request */
  81.     }
  82.     /* NOTREACHED */
  83. }
  84.